package com.softbank.speaker;

import org.eclipse.paho.client.mqttv3.MqttException;

import javax.swing.*;
import java.awt.event.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Speaker extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField textFieldDeviceSN;
    private JRadioButton radioButtonHapay;
    private JRadioButton radioButtonWX;
    private JRadioButton radioButtonZFB;
    private JRadioButton radioButtonUPay;
    private JLabel state;
    private static boolean exit = false;

    public Speaker() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        buttonOK.setEnabled(false);
        String deviceSN = textFieldDeviceSN.getText();
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        Date time = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = simpleDateFormat.format(time);
        String orderNo = System.currentTimeMillis() + "";
        try {
            if (radioButtonZFB.isSelected()) {
                SpeakerMQTTClient.getInstance().speak(
                        SpeakerMQTTClient.TransType.ORDER,
                        SpeakerMQTTClient.PayType.ZFBZF,
                        "560",
                        orderNo,
                        dateTime,
                        dateTime,
                        deviceSN
                );
                Thread.sleep(1000);
            }

            if (radioButtonHapay.isSelected()) {
                calendar = Calendar.getInstance(Locale.CHINA);
                time = calendar.getTime();
                dateTime = simpleDateFormat.format(time);
                orderNo = System.currentTimeMillis() + "";
                SpeakerMQTTClient.getInstance().speak(
                        SpeakerMQTTClient.TransType.ORDER,
                        SpeakerMQTTClient.PayType.HAPAY,
                        "1256",
                        orderNo,
                        dateTime,
                        dateTime,
                        deviceSN
                );

                Thread.sleep(1000);
            }
            if (radioButtonWX.isSelected()) {
                calendar = Calendar.getInstance(Locale.CHINA);
                time = calendar.getTime();
                dateTime = simpleDateFormat.format(time);
                orderNo = System.currentTimeMillis() + "";
                SpeakerMQTTClient.getInstance().speak(
                        SpeakerMQTTClient.TransType.ORDER,
                        SpeakerMQTTClient.PayType.WXZF,
                        "1256",
                        orderNo,
                        dateTime,
                        dateTime,
                        deviceSN
                );
                Thread.sleep(1000);
            }

            if (radioButtonUPay.isSelected()) {
                calendar = Calendar.getInstance(Locale.CHINA);
                time = calendar.getTime();
                dateTime = simpleDateFormat.format(time);
                orderNo = System.currentTimeMillis() + "";
                SpeakerMQTTClient.getInstance().speak(
                        SpeakerMQTTClient.TransType.ORDER,
                        SpeakerMQTTClient.PayType.UPSMPAY,
                        "8999",
                        orderNo,
                        dateTime,
                        dateTime,
                        deviceSN
                );
            }
        } catch (MqttException | InterruptedException e) {
            e.printStackTrace();
        } finally {
            buttonOK.setEnabled(true);
        }
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
        exit = true;
    }

    public static void main(String[] args) {
        System.out.println("开始啦~~~");
        try {
            SpeakerMQTTClient.init(
                    "post-cn-6ja1t8wk10b",
                    "post-cn-6ja1t8wk10b.mqtt.aliyuncs.com",
                    /*"LTAI4GBPuETdmT5fqyteNcAp"*/"LTAI4G1JhNhpxWru2QKyV1v1",
                    /*"tOdUSjuzXfimPIJK6ANFkw4conr8FM"*/"1kTY3z7S1szSW5uZQQvfCyqeNbMIRA",
                    "cloud_speaker",
                    "GID_CLOUD_SPEAKER",
                    "GID_CLOUD_SPEAKER" + "@@@OrderServer2020", 0);
        } catch (MqttException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        Speaker dialog = new Speaker();
        dialog.pack();
        dialog.setVisible(true);
        dialog.setTitle("云喇叭测试终端");
        while (!exit) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("完了~~~");
        System.exit(0);
    }
}
